package com.neusoft.elm.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.neusoft.elm.dao.AdminDao;
import com.neusoft.elm.po.Admin;
import com.neusoft.elm.util.DBUtil;

public class AdminDaoImpl implements AdminDao{
	
	private Connection con = null;
	private PreparedStatement pst = null;
	private ResultSet rs = null;

	@Override
	public Admin getAdminByNameByPass(String adminName,String password) {
	    Admin admin = null;
	    String sql = "select * from admin where adminName=? and password=?";
	    	
	    	try {
	    		
	    		con = DBUtil.getConnection();
				pst = con.prepareStatement(sql);
				pst.setString(1, adminName);
		    	pst.setString(2, password);
		    	rs = pst.executeQuery();
		    	while(rs.next()) {
		    		admin = new Admin();
		    		admin.setAdminId(rs.getInt("adminId"));
		    		admin.setAdminName(rs.getString("adminName"));
		    		admin.setPassword(rs.getString("password"));
		    		}
	    	}catch (SQLException e) {
				e.printStackTrace();
			}finally {
				DBUtil.close(rs, pst, con);
			}
	    	return admin;
	}
	@Override
	public boolean updateAdminPassword(String adminName, String newPassword) {
	    String sql = "UPDATE admin SET password = ? WHERE adminName = ?";
	    try (Connection con = DBUtil.getConnection();
	         PreparedStatement pst = con.prepareStatement(sql)) {
	        pst.setString(1, newPassword);
	        pst.setString(2, adminName);
	        int rowsAffected = pst.executeUpdate();
	        return rowsAffected > 0;
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return false;
	}
	
		public boolean updateAdminName(String adminName, String newAdminName) {
			String sql = "UPDATE admin SET adminName = ? WHERE adminName = ?";
			try (Connection con = DBUtil.getConnection();
			         PreparedStatement pst = con.prepareStatement(sql)) {
			        pst.setString(1, newAdminName);
			        pst.setString(2, adminName);
			        int rowsAffected = pst.executeUpdate();
			        return rowsAffected > 0;
			    } catch (SQLException e) {
			        e.printStackTrace();
			    }
			    return false;
		}  
}
